﻿@page "/authentications/{action}"
@layout SimpleIdServer.IdServer.Website.Shared.MainLayout
@attribute [Authorize("Authenticated")]
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.AuthMethodsStore;
@using SimpleIdServer.IdServer.Website.Stores.IdProviderStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;
@inject IDispatcher dispatcher
@inject IUrlHelper urlHelper
@inject NavigationManager navigationManager
@inherits Fluxor.Blazor.Web.Components.FluxorLayout

<SidAuthorizeView Roles=@($"/authentication/manage,/authentication/view")>
    <Authorized>
        <div class="mt-3 mb-3">
            <RadzenText class="no-margin" Text="@Global.Authentications" TextStyle="TextStyle.DisplayH3" />
        </div>

        <RadzenCardLoader Variant="Variant.Filled" IsLoading=@isLoading>
            <RadzenTabs Change="@OnChange" @bind-SelectedIndex="selectedIndex">
                <Tabs>
                    <RadzenTabsItem Text="@Global.AuthenticationMethods">
                        <AuthMethods></AuthMethods>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.ExternalIdentityProviders">
                        <IdProviders></IdProviders>
                    </RadzenTabsItem>
                    <RadzenTabsItem Text="@Global.LockAccount">
                        <LockAccount></LockAccount>
                    </RadzenTabsItem>
                </Tabs>
            </RadzenTabs>
        </RadzenCardLoader>
    </Authorized>
    <NotAuthorized>
        @Global.NotAuthorized
    </NotAuthorized>
</SidAuthorizeView>

@code {
    Dictionary<string, int> mappingActionNameToIndex = new Dictionary<string, int>
    {
        { "authmethods", 0 },
        { "idproviders", 1 },
        { "lockaccount", 2 }
    };

    [Parameter]
    public string? action { get; set; } = null;
    int selectedIndex = 0;
    private bool isLoading { get; set; } = true;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            SubscribeToAction<GetAllAuthMethodSuccessAction>((act) =>
            {
                isLoading = false;
            });
            SubscribeToAction<SearchIdProvidersSuccessAction>((act) =>
            {
                isLoading = false;
            });
            SubscribeToAction<GetUserLockingOptionsSuccessAction>((act) =>
            {
                isLoading = false;
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (string.IsNullOrWhiteSpace(action)) return;
        if (!string.IsNullOrWhiteSpace(action) && mappingActionNameToIndex.ContainsKey(action))
            selectedIndex = mappingActionNameToIndex[action];
    }

    void OnChange(int index)
    {
        var rec = mappingActionNameToIndex.Single(kvp => kvp.Value == index);
        navigationManager.NavigateTo(urlHelper.GetUrl($"/authentications/{rec.Key}"));
    }
}